package com.application.lucky.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.application.lucky.Lucky;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by hemeng on 2017/11/8.
 * <p>
 * 维护每个请求的 版本号
 * <p>
 * 类似 startup_page
 */

public class DataVersion {

    private static final String CACHE_NAME = "http_data_cache";

    public static void saveCache(String key, String version) {
        SharedPreferences sharedPreferences = Lucky.getApplication().getSharedPreferences(CACHE_NAME, Context.MODE_PRIVATE);
        sharedPreferences.edit().putString(key, version).apply();

    }

    public static String getCache(String key) {
        SharedPreferences sharedPreferences = Lucky.getApplication().getSharedPreferences(CACHE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    public static void clearCache() {
        SharedPreferences sharedPreferences = Lucky.getApplication().getSharedPreferences(CACHE_NAME, Context.MODE_PRIVATE);
        sharedPreferences.edit().clear().apply();

    }

    /**
     * 获取一组数据，数据量小的请求，可以合并，目前知道的有 静态资源
     */
    public static Map<String, String> getCaches(List<String> keys) {

        Map<String, String> data = new HashMap<>();

        for (String key : keys) {
            data.put(key, getCache(key));
        }
        return data;
    }


    public static void saveCache(Map<String, String> data) {
        if (data == null || data.isEmpty()) {
            return;
        }
        Set<String> set = data.keySet();
        for (String key : set) {
            saveCache(key, data.get(key));
        }
    }
}
